package com.xf.day05

import scala.collection.mutable.{ListBuffer, Set}

// 可变的列表
object TestMutableList {
  def main(args: Array[String]): Unit = {
    testAddAndUpdate()
    // deleteElements(LB1)

    val mutableSet = Set( 1, 7, 3, 5, 2 )
    mutableSet.add(4)
    print(mutableSet)
  }

  private def testAddAndUpdate(): Unit = {
    val x = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
    x += 10
    println(x) // ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    x ++= List(11, 12, 13)
    println(x) // ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)

    x ++= Array('b'.toInt, 'c'.toInt)
    println(x) // ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 98, 99)

    x.insert(0, 6)
    println(x) // ListBuffer(6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 98, 99)

    x(0) = 999
    println(x) // ListBuffer(999, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 98, 99)
  }

  // 删除元素
  private def deleteElements(): Unit = {
    // 可变的列表
    val LB1 = ListBuffer(1, 2, 3, 4, 5, 6)
    //按值 删除列表元素
    LB1 -= (3, 4, 5)
    println(LB1) // ListBuffer(1, 2, 6)

    //按照位置删除列表元素
    LB1.remove(0)
    println(LB1) // ListBuffer(2, 6)

    val course = ListBuffer("Scala", "Python")
    println(course) // ListBuffer(Scala, Python)

    val x = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
    // x.remove(2, 3)
    println(x) // ListBuffer(1, 2, 6, 7, 8, 9)

    x --= Seq(1, 2, 6)
    println(x)
  }
}
